
#include "MemImage.h"

CMemImage::CMemImage(void):m_Base(0),
                           m_hProcess(NULL)
{
    SetOpenMode(TRUE);
}

CMemImage::CMemImage(
    IN HANDLE hProcess,
    IN LPCTSTR lpcszModName
)
{
    SetOpenMode(TRUE);
    SetProcessModule(hProcess, lpcszModName);
}

CMemImage::~CMemImage(void)
{
}

BOOL
CMemImage::SetProcessModule(
    IN HANDLE hProcess,
    IN LPCTSTR lpcszModName
)
{
    HMODULE hMod;
    hMod = GetModuleHandle(lpcszModName);
    
    m_hProcess = hProcess;

    if (hMod){
        m_Base = (ULONG)hMod;
        return TRUE;
    }
    return FALSE;
}

BOOL
CMemImage::PeOpen()
{
    
    if (!m_Base){
        return FALSE;
    }

    return TRUE;
}

BOOL
CMemImage::PeRead(
    IN ULONGLONG Offset, 
    OUT PVOID Buffer, 
    IN ULONG Length
)
{
    
    ULONG ReadSize;

    //
    //  call ReadProcessMemory.
    //
    
    if (!ReadProcessMemory(m_hProcess, (LPCVOID)(Offset + m_Base), Buffer, Length, &ReadSize)){
        
        //
        //  TODO log error.
        //
        
        LoggingErrorString(_T("ReadProcessMemory error code %d\b"), GetLastError());

        return FALSE;
    }
    
    //
    //  is ok?
    //
    
    return (ReadSize == Length ? TRUE : FALSE);

}

BOOL
CMemImage::PeWrite(
    IN ULONGLONG Offset, 
    OUT PVOID Buffer, 
    IN ULONG Length
)
{
    return TRUE;
}

VOID
CMemImage::PeClose(
    VOID
)
{

}